package br.com.examCertification.dao;

import java.util.List;

import br.com.examCertification.dao.OpenHelper;
import br.com.examCertification.dao.impl.PerguntaDao;
import br.com.examCertification.dao.impl.RespostaDao;
import br.com.examCertification.dao.impl.TipoCertificacaoDao;
import br.com.examCertification.dao.impl.TipoExameDao;
import br.com.examCertification.dao.tabledefinition.PerguntaTableDefinition;
import br.com.examCertification.dao.tabledefinition.RespostaTableDefinition;
import br.com.examCertification.dao.tabledefinition.TipoCertificacaoTableDefinition;
import br.com.examCertification.dao.tabledefinition.TipoExameTableDefinition;
import br.com.examCertification.model.Pergunta;
import br.com.examCertification.model.Resposta;
import br.com.examCertification.model.TipoCertificacao;
import br.com.examCertification.model.TipoExame;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.SystemClock;

public class DataManager {

	private Context context;
	private SQLiteDatabase database;
	private TipoCertificacaoDao tipoCertificacaoDAO;
	private TipoExameDao tipoExameDAO;
	private PerguntaDao perguntaDao;
	private RespostaDao respostaDao;
	
	public DataManager(Context context){
		setContext(context);
		SQLiteOpenHelper openHelper = new OpenHelper(context, "CERT", null, 2);
		setDatabase(openHelper.getWritableDatabase());
		this.setTipoExameDAO(new TipoExameDao(new TipoExameTableDefinition(), database));
		this.setTipoCertificacaoDAO(new TipoCertificacaoDao(new TipoCertificacaoTableDefinition(), database));
		this.setPerguntaDao(new PerguntaDao(new PerguntaTableDefinition(), database));
		this.setRespostaDao(new RespostaDao(new RespostaTableDefinition(), database));
	}
	
	private void openDb(){
		if(! getDatabase().isOpen()){														
			setDatabase(SQLiteDatabase.openDatabase(Environment.getDataDirectory() + "/data/br.com.examCertification/databases/CERT.db", null, SQLiteDatabase.OPEN_READWRITE));
		}
	}
	
	private void closeDb() {
		if (getDatabase().isOpen()) {
			getDatabase().close();
	    }
	}

	private void resetDb() {      
		closeDb();
	    SystemClock.sleep(500);
	    openDb();
	}
	
	
	public TipoCertificacao getTipoCertificacao(Long id){		
		return getTipoCertificacaoDAO().get(id);
	}
	
	public TipoExame getTipoExame(Long id){
		return getTipoExameDAO().get(id);
	}
	
	
	
	public TipoExame getTipoExameByName(String name){
		return getTipoExameDAO().getByClause(" NAME ='"+name+"'", null, null, null, "1");
	}
	
	
	public Pergunta getPerguntaById(Long id){
		
		return getPerguntaDao().getByClause(" _id ='"+id+"'", null, null, null, "1");
	}
	
	

	public List<TipoCertificacao> getTipoCertificacaoList(){
		return getTipoCertificacaoDAO().getAll();
	}
	
	public List<TipoExame> getTipoExameList(){
		return getTipoExameDAO().getAll();
	}	
	
	public List<Pergunta> getPerguntaList(){
		return getPerguntaDao().getAll();
	}	
	
	public List<TipoExame> getTipoExameListById(long l){
		return getTipoExameDAO().getAllbyClause(" tipoCertificao_id ='"+l+"'", null, null, null, null);
	}	
	
	public List<Pergunta> getPerguntasById(long l){
		return getPerguntaDao().getAllbyClause(" tipoExame_id ='"+l+"'", null, null, null, null);
	}	
	
	public List<Resposta> getRespostasById(long l){
		return getRespostaDao().getAllbyClause(" pergunta_id ='"+l+"'", null, null, null, null);
	}	
	
	public boolean deleteTipoCertificacao(Long id){
		boolean result = false;
		getDatabase().beginTransaction();
		result = getTipoCertificacaoDAO().delete(id.intValue());
		getDatabase().setTransactionSuccessful();
		getDatabase().endTransaction();
		return result;
	}
	
	public boolean deleteTipoExame(Long id){
		boolean result = false;
		getDatabase().beginTransaction();
		result = getTipoExameDAO().delete(id.intValue());
		getDatabase().setTransactionSuccessful();
		getDatabase().endTransaction();
		return result;
	}
	
	public long saveTipoCertificacao(TipoCertificacao game){
		long result = 0;		
		try {
			getDatabase().beginTransaction();
			result = getTipoCertificacaoDAO().save(game);
			getDatabase().setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		}		
		getDatabase().endTransaction();
		return result;
	}
	
	public long savePergunta(Pergunta pergunta){
		long result = 0;		
		try {
			getDatabase().beginTransaction();
			result = getPerguntaDao().save(pergunta);
			getDatabase().setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		}		
		getDatabase().endTransaction();
		return result;
	}
	
	public long saveResposta(Resposta resposta){
		long result = 0;		
		try {
			getDatabase().beginTransaction();
			result = getRespostaDao().save(resposta);
			getDatabase().setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		}		
		getDatabase().endTransaction();
		return result;
	}
	
	public long saveTipoExame(TipoExame platform){
		long result = 0;		
		try {
			getDatabase().beginTransaction();
			result = getTipoExameDAO().save(platform);
			getDatabase().setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		}		
		getDatabase().endTransaction();
		return result;
	}
	
	public boolean updateTipoCertificacao(TipoCertificacao game){
		boolean result = false;
		try {
		getDatabase().beginTransaction();
		getTipoCertificacaoDAO().update(game, game.getId());
		getDatabase().setTransactionSuccessful();
		result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}		
		getDatabase().endTransaction();
		return result;
	}
	
	public boolean updateTipoExame(TipoExame platform){
		boolean result = false;
		try {
		getDatabase().beginTransaction();
		getTipoExameDAO().update(platform, platform.getId());
		getDatabase().setTransactionSuccessful();
		result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}		
		getDatabase().endTransaction();
		return result;
	}
	
	public Context getContext() {
		return context;
	}
	public void setContext(Context context) {
		this.context = context;
	}
	public SQLiteDatabase getDatabase() {
		return database;
	}
	public void setDatabase(SQLiteDatabase database) {
		this.database = database;
	}

	public void setTipoCertificacaoDAO(TipoCertificacaoDao tipoCertificacaoDAO) {
		this.tipoCertificacaoDAO = tipoCertificacaoDAO;
	}

	public TipoCertificacaoDao getTipoCertificacaoDAO() {
		return tipoCertificacaoDAO;
	}

	public void setTipoExameDAO(TipoExameDao tipoExameDAO) {
		this.tipoExameDAO = tipoExameDAO;
	}

	public TipoExameDao getTipoExameDAO() {
		return tipoExameDAO;
	}

	public void setPerguntaDao(PerguntaDao perguntaDao) {
		this.perguntaDao = perguntaDao;
	}

	public PerguntaDao getPerguntaDao() {
		return perguntaDao;
	}


	public void setRespostaDao(RespostaDao respostaDao) {
		this.respostaDao = respostaDao;
	}

	public RespostaDao getRespostaDao() {
		return respostaDao;
	}


	
}
